package comman.duke 
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import morn.core.components.Image;
	/**
	 * ...
	 * @author jerry.d
	 */
	public class ImageClickCenter 
	{
		
		public function ImageClickCenter() 
		{
			GameVars.STAGE.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		private var buttons:Vector.<DisplayObject> = new Vector.<DisplayObject>();
		private var currentButton:DisplayObject;
		public function add(button:DisplayObject):void{
			if ( buttons.indexOf(button) == -1){
				/**
				button.anchorX = button.anchorY = 0.5;
				button.x += button.width >> 1;
				button.y += button.height >> 1;
				*/
				button.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
				
				
			}
		}
		
		public function remove(button:DisplayObject):void{
			var index:int =  buttons.indexOf(button);
			if ( index != -1){
				/**
				buttons.splice(index, 1);
				button.anchorX = button.anchorY = 0;
				button.x -= button.width >> 1;
				button.y -= button.height >> 1;
				*/
				button.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			}
		}
		
		private function onMouseDown(evt:MouseEvent):void{
			currentButton = evt.currentTarget as DisplayObject;
			currentButton.scaleX = currentButton.scaleY = 0.9;
		}
		
		private function onMouseUp(evt:MouseEvent):void{
			if( currentButton != null)
				currentButton.scaleX = currentButton.scaleY = 1;
		}
		
		private static var _instance:ImageClickCenter;
		public static function get Instance():ImageClickCenter{
			if ( ImageClickCenter._instance == null){
				ImageClickCenter._instance = new ImageClickCenter();
			}
			return ImageClickCenter._instance;
		}
	}

}